import org.devops.git
import org.devops.dingmes
import org.devops.mvnbuild
import org.devops.dockerbuild
import org.devops.deploy

def call(Map map){
    def private g = new git()
    def private mb = new mvnbuild()
    def private dm = new dingmes()
    def private dockerb = new dockerbuild()
    def private dp = new deploy()
    def image_name = map.registry+'/'+map.image_name+':'+map.project_name+'-'+params.ENV_TYPE+'-'+env.BUILD_ID
    def deploy_path = map.d_compose_path+'/'+params.ENV_TYPE+'/'+map.project_name
    def ansible_host = map.ansible_host+'-'+params.ENV_TYPE
    pipeline {
        agent{
            kubernetes{
                inheritFrom 'maven-3.6'
            }
        }
        // agent any
        parameters {
            string(name: 'PROJECT_NAME',
                defaultValue:  map.project_name,
                description: '项目名称')

            choice(name: 'ENV_TYPE', 
                choices: ['dev', 'test', 'pro','pre'], 
                description: '部署环境：dev:开发环境; test:测试环境; pre:预发布环境; pro: 生产环境')

            string(name: 'PROJECT_URL',
                defaultValue: map.git_url, 
                description: '项目仓库地址')

            gitParameter (
                    defaultValue: map.git_default_branch,
                    branchFilter: 'origin/(.*)',
                    name: 'BRANCH',
                    quickFilterEnabled: true,
                    selectedValue: 'DEFAULT',
                    sortMode: 'DESCENDING_SMART',
                    tagFilter: '*',
                    type: 'PT_BRANCH',
                    description: 'Please select a branch or tag to build',
                    useRepository: map.git_url
            )
        }

        stages{
            stage('git clone') {
                steps{
                    script{
                        BRANCH_TAG = sh(returnStdout: true,script: "echo ${params.BRANCH_TAG}|awk -F '/' '{print \$NF}'").trim()
                        g.gitco(params.BRANCH, map.git_credentials_id, map.git_url)
                    }
                }
            }

            stage('maven build') {
                steps {
                    container('maven'){
                        script{
                            echo "[INFO] 构建参数: $map.mvn_param  环境: $ENV_TYPE"
                            if ( map.mvn_param == "") {
                                mb.mavenbuild(map.mvn_param)
                            }
                            if ( map.mvn_dev_param == "-Ptest" &&  ENV_TYPE == "dev" ) {
                                mb.mavenbuild(map.mvn_dev_param)
                            }
                            if ( map.mvn_test_param == "-Ptest" &&  ENV_TYPE == "test" ) {
                                mb.mavenbuild(map.mvn_test_param)
                            }
                            if ( map.mvn_pro_param == "-Pproduction" &&  ENV_TYPE == "pro" ) {
                                mb.mavenbuild(map.mvn_pro_param)
                            }
                        }
                    }
                }
            }
            stage('docker build'){
                steps {
                    script{
                        echo "[INFO] dockerfile: $map.dockerfilename  image_name: $image_name"
                        dockerb.generateDockerfile(map.dockersvc_type,map.dockerfilename)  // 生成dockerfile
                        dockerb.dockerbuild(map.dhub_cred,image_name,map.registry)
                    }
                }
            } 
            stage('deploy'){
                parallel {
                    stage('deploy dev'){
                        // 测试环境部署
                        when {
                            expression { params.ENV_TYPE == "dev" }  
                            // expression { BRANCH_TAG ==~ /(test)/ }
                        }
                        steps {
                            script{
                                echo "dev 部署方式: $map.dev_deploy"
                                if ( map.dev_deploy == 'k8s' ) {
                                    dp.k8sDeploy(map.k8s_conn_yaml,params.ENV_TYPE,image_name,map.deploy_yaml)
                                }
                                if ( map.dev_deploy == 'ansible' ) {
                                    container('ansible'){
                                      dp.ansibleMvnDeploy(map.ansible_hosts_file, ansible_host,image_name, deploy_path)
                                    }
                                }
                                if ( map.dev_deploy == 'ssh' ) {
                                    dp.sshDockerDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, map.dhub_cred, map.registry,image_name,deploy_path)
                                }
                                if ( map.dev_deploy == 'nodeploy' ) {
                                    echo ansible_host
                                    throw new Exception("您选择的环境未配置部署方式！！！,请联系运维人员处理")
                                }                                
                            }
                        }
                    }
                    stage('deploy test'){
                        // 测试环境部署
                        when {
                            expression { params.ENV_TYPE == "test" }  
                            // expression { BRANCH_TAG ==~ /(test)/ }
                        }
                        steps {
                            script{
                                echo "test 部署方式: $map.test_deploy"
                                if ( map.test_deploy == 'k8s' ) {
                                    dp.k8sDeploy(map.k8s_conn_yaml,params.ENV_TYPE,image_name,map.deploy_yaml)
                                }
                                if ( map.test_deploy == 'ansible' ) {
                                    container('ansible'){
                                      dp.ansibleMvnDeploy(map.ansible_hosts_file, ansible_host,image_name, deploy_path)
                                    }                                
                                }
                                if ( map.test_deploy == 'ssh' ) {
                                    dp.sshDockerDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, map.dhub_cred, map.registry,image_name,deploy_path)
                                }
                                if ( map.test_deploy == 'nodeploy' ) {
                                    throw new Exception("您选择的环境未配置部署方式！！！,请联系运维人员处理")
                                }   
                            }
                        }
                    }
                    stage('deploy pre'){
                        // pre环境部署
                        when {
                            expression { params.ENV_TYPE == "pre" }  
                            // expression { BRANCH_TAG ==~ /(test)/ }
                        }
                        steps {
                            script{
                                echo "test 部署方式: $map.pre_deploy"
                                if ( map.pre_deploy == 'k8s' ) {
                                    dp.k8sDeploy(map.k8s_conn_yaml,params.ENV_TYPE,image_name,map.deploy_yaml)
                                }
                                if ( map.pre_deploy == 'ansible' ) {
                                    container('ansible'){
                                      dp.ansibleMvnDeploy(map.ansible_hosts_file, ansible_host,image_name, deploy_path)
                                    }
                                }
                                if ( map.pre_deploy == 'ssh' ) {
                                    dp.sshDockerDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, map.dhub_cred, map.registry,image_name,deploy_path)
                                }
                                if ( map.pre_deploy == 'nodeploy' ) {
                                    throw new Exception("您选择的环境未配置部署方式！！！,请联系运维人员处理")
                                }  
                            }
                        }
                    }
                    stage('deploy pro'){
                        // pro环境部署
                        when {
                            allOf {
                                expression { params.ENV_TYPE == "pro" }            
                            }
                            anyOf {
                                expression { env.BUILD_USER == "admin" }
                                expression { env.BUILD_USER == "zhangyu" }
                            }
                        }
                        steps {
                            script{
                                echo "test 部署方式: $map.pro_deploy"
                                if ( map.pro_deploy == 'k8s' ) {
                                    dp.k8sDeploy(map.k8s_conn_yaml,params.ENV_TYPE,image_name,map.deploy_yaml)
                                }
                                if ( map.pro_deploy == 'ansible' ) {
                                    container('ansible'){
                                      dp.ansibleMvnDeploy(map.ansible_hosts_file, ansible_host,image_name, deploy_path)
                                    }
                                }
                                if ( map.pro_deploy == 'ssh' ) {
                                    dp.sshDockerDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, map.dhub_cred, map.registry,image_name,deploy_path)
                                }
                                if ( map.pre_deploy == 'nodeploy' ) {
                                    throw new Exception("您选择的环境未配置部署方式！！！,请联系运维人员处理")
                                }  
                            }
                        }
                    }
                }
            }		 
	   }
        post {
            // success {
            //     script {
            //         dm.DingdingReq(map.robotId, "构建成功 ✅")
            //     }
            // }
            failure {
                script {
                    dm.DingdingReq(map.robotId, "构建失败 ❌")
                }
            }
        }
    }
}

